import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { NavController,IonInfiniteScroll } from '@ionic/angular';
import { ConfigService } from 'src/app/service/config.service';
import { HttpService } from 'src/app/service/http.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { ToolService } from 'src/app/service/tool.service';
import { UrlService } from 'src/app/service/url.service';

@Component({
  selector: 'app-fklist',
  templateUrl: './fklist.page.html',
  styleUrls: ['./fklist.page.scss'],
})
export class FklistPage implements OnInit {
  private page:number=1;
  public disabled:boolean=false;
  public isLastFk=false;
  public lastFk:object=null;
  public listFk=[];
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  constructor(private navCtrl:NavController,private http:HttpService,private popWin:PopwinService,private router:Router) { }

  ngOnInit() {
    this.listNet();
  }

  listNet(page=1,infinite?: any, isMore: boolean = false){
    let that = this;
    let obj = "{\"page\":\"" + page + "\",\"pageSize\":\"10\"}";
    this.http.get(UrlService.OTHER_FEEDBACKLIST, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      if (infinite) {
        that.infiniteScroll.complete();
      }
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        if (!isMore) {//下拉或第一次加载
            if(ToolService.isEmpty(res.newlist)){
               that.isLastFk=false;
               that.lastFk=null;
            }else{
               that.isLastFk=true;
               that.lastFk=res.newlist;
            }
            that.listFk=res.list;
            that.disabled = false;
        } else {
          if (res.list.length > 0) {
            that.listFk = that.listFk.concat(res.list);
            console.log("有下滑!!!");
          } else {
             that.infiniteScroll.disabled = true;
             that.disabled = true;
             that.popWin.toast("数据已经加载完成");
          }
        }
        console.log("---------------",res);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
      if (infinite) {
        that.infiniteScroll.complete();
      }
    });
  }

  loadData(event) {
    this.page++;
    this.listNet(this.page, event, true);
  }

  onFkDetail(feeID){
      let param = "{\"fid\":\"" + feeID + "\"}";
      this.router.navigate(['/fkdetail'], {
        queryParams: { "p": ToolService.paramAESEncrypt(param)}
      });
  }

  backClick() {
    this.navCtrl.navigateBack("tabs/my");
  }
}
